package com.dianxin.farm.util.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class PdfUtil {

    private static int blank = 5;

    public static void main(String[] args) {
        try {
            // 获取所有图片
         /*   List<String> list = new ArrayList<>();
            list.add("D:\\photo\\sign.png");
            addPdfMark("D:\\photo\\test.pdf", "D:\\photo\\target.pdf", list);*/

            imagePdf("D:\\photo\\sign.png","D:\\photo\\test.pdf");
        } catch (Exception e) {
            System.out.println("失败");
            e.printStackTrace();
        }
        System.out.println("成功");
    }

    public static void addPdfMark(String InPdfFile, String outPdfFile, List<String> imgList) throws Exception {

		try {
        // 获取PDF文档信息
        Map<String, Object> pdfMsg = getPdfMsg(InPdfFile);
        // 开始计算图片起始位置 * PDF文档宽度 - （所有图片的宽度 + 每张图片右侧加5 个单位的空白 ）
        float startAddress = Float.valueOf(pdfMsg.get("width").toString());
        for (String imgPath : imgList) {
            startAddress -= (Double.valueOf(getImgMsg(imgPath).get("width").toString()) + blank);
        }

        PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes());
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile));

        for (String imgPath : imgList) {
            Image img = Image.getInstance(imgPath);// 插入水印
            // 设置图片水印的位置。
            img.setAbsolutePosition(startAddress, 400);
            // 开始水印 如果需要每一页都加图片，这里添加循环即可
            PdfContentByte under = stamp.getUnderContent(Integer.valueOf(pdfMsg.get("pageSize").toString()));
            under.addImage(img);
            startAddress += Float.valueOf(Float.valueOf(getImgMsg(imgPath).get("width").toString()) + blank);
        }

        stamp.close();// 关闭
        File tempfile = new File(InPdfFile);

        if (tempfile.exists()) {
            tempfile.delete();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

    public static Map<String, Object> getPdfMsg(String filePath) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        try {
            // 获取PDF共有几页
            PdfReader pdfReader = new PdfReader(new FileInputStream(filePath));
            int pages = pdfReader.getNumberOfPages();
            // System.err.println(pages);
            map.put("pageSize", pages);

            // 获取PDF 的宽高
            PdfReader pdfreader = new PdfReader(filePath);
            Document document = new Document(pdfreader.getPageSize(pages));
            float widths = document.getPageSize().getWidth();
            // 获取页面高度
            float heights = document.getPageSize().getHeight();
            // System.out.println("widths = " + widths + ", heights = " + heights);
            map.put("width", widths);
            map.put("height", heights);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public static Map<String, Object> getImgMsg(String imgPath) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        try {
            File picture = new File(imgPath);
            BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));
            // System.out.println("=源图宽度===>"+sourceImg.getWidth()); // 源图宽度
            // System.out.println("=源图高度===>"+sourceImg.getHeight()); // 源图高度
            map.put("width", sourceImg.getWidth());
            map.put("height", sourceImg.getHeight());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 功能描述:
     * @Description:
     * @param urljPG 水印图片
     * @param urlPdf PDF路径
     * @Author: Mr.Jie
     */
    public static void imagePdf(String urljPG, String urlPdf) throws Exception {
        // 获取去除后缀的文件路径
        String fileName = urlPdf.substring(0, urlPdf.lastIndexOf("."));
        //把截取的路径，后面追加四位随机数
        String pdfUrl = fileName + (int) ((Math.random() * 9 + 1) * 1000) + ".pdf";
        //要加水印的原pdf文件路径
        PdfReader reader = new PdfReader(urlPdf, "PDF".getBytes());
        //加了水印后要输出的路径
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(pdfUrl));
        // 插入水印
        Image img = Image.getInstance(urljPG);
        //原pdf文件的总页数
        int pageSize = reader.getNumberOfPages();
        //印章位置
        img.setAbsolutePosition(278, 100);
        //印章大小
        img.scalePercent(50);
        for (int i = 1; i <= pageSize; i++) {
            //背景被覆盖
          // PdfContentByte under = stamp.getUnderContent(i);
            //文字被覆盖
            PdfContentByte under = stamp.getOverContent(i);
            //添加电子印章
            under.addImage(img);
        }
        // 关闭
        stamp.close();
        //关闭
        reader.close();
    }




}
